<?php
/**
 * User: 魔十七
 * Date: 2020/11/9
 * Time: 13:30
 */
namespace Merchant\Controller;

use Think\Controller;
use Admin\Controller\UploadController;
class CouponController extends  Controller
{
    public function index(){

        $this->display();
    }

    /**
     * 购物券列表
     * @param  $id      商铺ID
     * @param  $page    分页
     * */
    public function CouponLists()
    {
        $id     = session('merchant.id');
        $page   = $_GET['page'];
        $couponList = M('merchant_coupon')
            ->where("shop_id=$id")
            ->order('id desc')
            ->page($page.',20')
            ->select();

        $count = M('merchant_coupon')->where('shop_id','=',$id)->order('id desc')->count();
        if ($count){
            $data = ['code'=>'0','msg'=>'','count'=>$count,'data'=>$couponList];
        }else{
            $data = ['code'=>'1','msg'=>'没有优惠券!'];
        }
        echo json_encode($data);
    }

    /**
     * 添加优惠券
     * @param $id   店铺ID
     * @param $uid  店铺uID
     */
    public function create()
    {
        $id     = session('merchant.id');       // 店铺ID
        $uid    = session('merchant.uid');      // 店铺uID
        $time = date('Y-m-d H:i:s',time());
        $detection = M('merchant')->where('id','eq',$id)->where('uid','eq',$uid)->find();
        if ($detection){
            $couponType = array('id' => '1', 'name' => '店铺优惠券');
        }else{
            $couponType = array('id' => '2', 'name' => '平台优惠券');
        }
        $this->assign(
            array(
                'id'            => $id,
                'uid'           => $uid,
                'time'          => $time,
                'couponType'    => $couponType
            )
        );
        $this->display();
    }

    public function save()
    {
        $id = isset($_POST['id'])? $_POST['id'] : '';
        $timeStr = explode(',',$_POST['start_time']);       // 优惠券开始与结束时间
        if ($_POST['quota'] <= 0){ $quota = '10'; } else { $quota = $_POST['quota']; }

        $data = array(
            'coupon_type'   => $_POST['coupon_type'],
            'title'         => $_POST['title'],
            'img'           => $_POST['img'],
            'with_amount'   => $_POST['with_amount'],
            'used_amount'   => $_POST['used_amount'],
            'quota'         => $quota,
            'take_count'    => $_POST['take_count'],
            'used_count'    => $_POST['used_count'],
            'start_time'    => $timeStr[0],
            'end_time'      => $timeStr[1],
            'bz'            => $_POST['bz'],
        );

        if ($id){
            unset($_POST['id']);
            $data['update_by']      = $_POST['m_uid'];
            $data['update_time']    = date('Y-m-d H:i:s',time());
            $CouponSave = M('merchant_coupon')->where("id=$id")->save($data);
        } else {
            $data['shop_id']        = $_POST['shop_id'];
            $data['create_by']      = $_POST['m_uid'];
            $data['create_time']    = $_POST['create_time'];
            $data['update_by']      = $_POST['m_uid'];
            $data['update_time']    = date('Y-m-d H:i:s',time());
            $CouponSave = M('merchant_coupon')->add($data);
        }
        if ($CouponSave){
            $go_back['code'] = 1001;
        } else {
            $go_back['code'] = 1002;
            $go_back['msg']  = 'error';
        }
        $this->ajaxReturn($go_back);
    }

    /**
     * 编辑优惠券
     * @param $id   ID
     */
    public function edit($id)
    {
        $time = date('Y-m-d H:i:s',time());
        $uid    = session('merchant.uid');      // 店铺uID
        $detection = M('merchant')->where('id','eq',$id)->where('uid','eq',$uid)->find();
        if ($detection){
            $couponType = array('id' => '1', 'name' => '店铺优惠券');
        }else{
            $couponType = array('id' => '2', 'name' => '平台优惠券');
        }
        $couponData = M('merchant_coupon')->where('id','eq',$id)->find();
        if($couponData){
            $this->assign('data',$couponData);
            $this->assign('time',$time);
            $this->assign(
                array(
                    'id'            => $id,
                    'uid'           => $uid,
                    'time'          => $time,
                    'couponType'    => $couponType,
                    'data'          => $couponData
                )
            );
            $this->display();
        }else{
            $this->error('id异常');
        }
    }

    /**
     * 暂时不写
     * 删除优惠券
     * */
    public function delete()
    {

    }

}